﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace auxiliary_proj
{
    public partial class FormViewInventory : Form
    {
        MySqlConnection connection;
        DataTable table;
        int item_id;
        //Default Constructor
        public FormViewInventory()
        {
        }
        //Parameterized Constructor
        public FormViewInventory(int item_id, string desc, double actual_price)
        {
            InitializeComponent();

            this.dataGridViewInventory.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewInventory_CellValueChange);

            connection = new MySqlConnection(Program.getConnection());
            this.lbItemNo.Text = "" + item_id;
            this.lbDescription.Text = desc;
            this.tbActualPrice.Text = "" + actual_price;
            this.item_id = item_id;
            query();
        }
        //Query that gets all the price and quantity for a specific item
        public void query()
        {
            string query = "SELECT price, quantity, timestamp FROM ITEM_QUANTITY where item_id = '" + item_id + "' order by timestamp DESC";
            try
            {
                connection.Open();
                try
                {
                    table = new DataTable();
                    table = Query.select(query);
                    ViewItemBindingSource.DataSource = table;
                    dataGridViewInventory.DataSource = ViewItemBindingSource;
                }
                catch (MySqlException exc) { MessageBox.Show(exc.Message); }
            }
            catch (DataException ex) { MessageBox.Show(ex.Message); }
            finally
            {
                connection.Close();
            }
        }
        //Cell Event Handler that updates the database if a value in changed in any cell (quantity or price)
        private void dataGridViewInventory_CellValueChange(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewInventory.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals(""))
            {
                MessageBox.Show("Input must not be empty!");
            }
            else
            {
                string update = "UPDATE ITEM_QUANTITY SET " + dataGridViewInventory.Columns[e.ColumnIndex].DataPropertyName + " = '" + dataGridViewInventory.Rows[e.RowIndex].Cells[e.ColumnIndex].Value +
                                  "' WHERE item_id = '" + item_id + "' AND timestamp = '" + ((System.DateTime)dataGridViewInventory.Rows[e.RowIndex].Cells["timestamp"].Value).ToString("yyyy-MM-dd HH:mm:ss") + "'";
                Query.nonQuery(update);
                query();
            }
        }
        //Method that calculates the actual price for a item
        public double average()
        {
            double total = 0;
            int totalQuantity = 0;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                total += (double)table.Rows[i]["price"] * (int)table.Rows[i]["quantity"];
                totalQuantity += (int)table.Rows[i]["quantity"];
            }
            return total / (double)totalQuantity;
        }
        //Sets the actual price to the textbox to be viewed
        private void btnCalculateAvg_Click(object sender, EventArgs e)
        {
            tbActualPrice.Text = "" + average().ToString("##.##");
        }
        //Updates the actual price for the item
        private void btnSavePrice_Click(object sender, EventArgs e)
        {
            string update = "UPDATE ITEM SET actual_price = '" + tbActualPrice.Text + "' WHERE item_id = '" + item_id + "'";
            Query.nonQuery(update);
            Singleton.getMainForm().qInventory();
            Singleton.getUCInventory().qInventory();
        }
    }
}
